package com.zhou.juc.test11_VarHandle;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/16-16:16
 */
public class T01_HelloVarHandle {
    int x = 8;

    /**
     *jdk1.9 版本
     * @VarHandle
     */
    private static VarHandle varHandle;

    static {
        try {
            varHandle = MethodHandles.lookup().findVarHandle(T01_HelloVarHandle.class, "x", int.class);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        T01_HelloVarHandle t = new T01_HelloVarHandle();
        System.out.println(varHandle.get(t));

        varHandle.set(t,9);
        System.out.println(t.x);

        varHandle.compareAndSet(t,9,10);
        System.out.println(t.x);

        varHandle.getAndAdd(t,10);
        System.out.println(t.x);

        varHandle.compareAndSet(t,20,10);
        System.out.println(t.x);
    }
}
